const mysql = require('../../core/mysql');

const log = require('../../core/logger').getLogger("system");
const moment = require('moment');
const common = require('../../core/common');
const stringUtils = require('../../core/util/StringUtils');
const _ = require('lodash');

exports.add = async(ctx) => {
    let model_type_name = ctx.request.body.model_type_name ? ctx.request.body.model_type_name.trim() : '';
    if (model_type_name === '') {
        ctx.body = {
            result: 201,
            message: "请检查参数"
        };
        return;
    }
    let newModelType = {
        model_type_name
    };
    try{
        let selectExistModelType = await mysql.query(`select * from model_type where model_type_name = ? `, model_type_name);
        if (selectExistModelType.length > 0){
            ctx.body = {
                result: 202,
                message: "已存在此设备类型"
            };
            return;
        }
        let ret = await mysql.query(`INSERT INTO model_type SET ?`, newModelType);
        if (!ret || ret.insertId === -1){
            ctx.body = {
                result: 202,
                message: "新增设备类型失败"
            };
        } else {
            ctx.body = {
                result: 200,
                id: ret.insertId,
                message: "新增设备类型成功"
            };
        }
    }catch (e) {
        ctx.body = {
            result: 203,
            message: "新增设备类型失败"+e.toString()
        };
    }
}

exports.delete = async(ctx) => {
    let model_type_id = ctx.params.model_type_id || 0;
    if (model_type_id <= 0) {
        ctx.body = {
            result: 201,
            message: "请检查参数"
        };
        return
    }
    try{
        //有关联都设备则不能删除
        let selectProducts = await mysql.query(`select COUNT(product_id) AS Total from product WHERE model_type_id = ? and is_deleted = 0 `, model_type_id);
        if (selectProducts[0].Total > 0){
            ctx.body = {
                result: 203,
                message: "删除失败,存在该类型设备"
            };
            return
        }
        ret = await mysql.query(`delete from model_type WHERE model_type_id = ?`, model_type_id);
        if (!ret || ret.affectedRows === 0){
            ctx.body = {
                result: 203,
                message: "对象不存在或已删除"
            };
        } else {
            ctx.body = {
                result: 200,
                message: "删除成功"
            };
        }
    }catch (e) {
        ctx.body = {
            result: 205,
            message: "错误"+e.toString()
        };
    }
}

exports.update = async(ctx) => {
    let model_type_id = ctx.request.body.model_type_id || 0;
    let model_type_name = ctx.request.body.model_type_name ? ctx.request.body.model_type_name.trim() : '';

    if (model_type_id <= 0 || model_type_name === '') {
        ctx.body = {
            result: 201,
            message: "请检查参数"
        };
    }else{
        try {
            let selectRows = await mysql.query(`SELECT * FROM model_type WHERE model_type_id = ?`, model_type_id);
            if (selectRows.length > 0) {
                let selectExistModelType = await mysql.query(`select * from model_type where model_type_name = ? and model_type_id <> ? `, [model_type_name,model_type_id]);
                if (selectExistModelType.length > 0){
                    ctx.body = {
                        result: 202,
                        message: "已存在此设备类型"
                    };
                    return;
                }
                selectRows[0].model_type_name = model_type_name;
                ret = await mysql.query(`UPDATE model_type SET ? WHERE model_type_id= ? `, [selectRows[0],model_type_id]);
                if (!ret || ret.insertId === -1) {
                    ctx.body = {
                        result: 202,
                        message: "更新失败"
                    };
                } else {
                    ctx.body = {
                        result: 200,
                        message: "更新成功"
                    };
                }
            } else {
                ctx.body = {
                    result: 203,
                    message: "对象不存在或者已删除"
                };
            }
        }catch (e) {
            ctx.body = {
                result: 204,
                message: "更新失败"+e.toString()
            };
        }
    }
}


exports.getList = async(ctx) => {
    let create_user_id = ctx.user.id || '';
    let counts = ctx.query.counts || 10;
    let indexfrom = ctx.query.indexfrom || 0;

    if (create_user_id === ''){
        ctx.body = {
            result: 201,
            message: "无效的用户id"
        };
    } else {
        try {
            let results = await mysql.query(`SELECT * FROM model_type ORDER BY model_type_id LIMIT ${indexfrom}, ${counts} ` );
            let totalResult = await mysql.query(`SELECT COUNT(model_type_id) AS total FROM model_type `);
            ctx.body = {
                result: 200,
                list:results,
                totalpage:Math.ceil((totalResult.length > 0 ? totalResult[0].total : 0)/counts),
                totalcounts:totalResult.length > 0 ? totalResult[0].total : 0,
                message: "查询成功"
            };
        }catch (e) {
            ctx.body = {
                result: 205,
                message: "错误 " + e.toString()
            };
        }
    }
}